package com.empire.module.member.service.mappolygonpoint;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.empire.module.member.controller.admin.mappolygonpoint.vo.*;
import com.empire.module.member.dal.dataobject.mappolygonpoint.MapPolygonPointDO;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.util.object.BeanUtils;

import com.empire.module.member.dal.mysql.mappolygonpoint.MapPolygonPointMapper;

import javax.annotation.Resource;

import static com.empire.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.empire.module.member.enums.ErrorCodeConstants.*;

/**
 * 多边形顶点坐标 Service 实现类
 *
 * @author 主席大人
 */
@Service
@Validated
public class MapPolygonPointServiceImpl implements MapPolygonPointService {

    @Resource
    private MapPolygonPointMapper mapPolygonPointMapper;

    @Override
    public Long createMapPolygonPoint(MapPolygonPointSaveReqVO createReqVO) {
        // 插入
        MapPolygonPointDO mapPolygonPoint = BeanUtils.toBean(createReqVO, MapPolygonPointDO.class);
        mapPolygonPointMapper.insert(mapPolygonPoint);
        // 返回
        return mapPolygonPoint.getId();
    }

    @Override
    public void updateMapPolygonPoint(MapPolygonPointSaveReqVO updateReqVO) {
        // 校验存在
        validateMapPolygonPointExists(updateReqVO.getId());
        // 更新
        MapPolygonPointDO updateObj = BeanUtils.toBean(updateReqVO, MapPolygonPointDO.class);
        mapPolygonPointMapper.updateById(updateObj);
    }

    @Override
    public void deleteMapPolygonPoint(Long id) {
        // 校验存在
        validateMapPolygonPointExists(id);
        // 删除
        mapPolygonPointMapper.deleteById(id);
    }

    private void validateMapPolygonPointExists(Long id) {
        if (mapPolygonPointMapper.selectById(id) == null) {
            throw exception(MAP_POLYGON_POINT_NOT_EXISTS);
        }
    }

    @Override
    public MapPolygonPointDO getMapPolygonPoint(Long id) {
        return mapPolygonPointMapper.selectById(id);
    }

    @Override
    public PageResult<MapPolygonPointDO> getMapPolygonPointPage(MapPolygonPointPageReqVO pageReqVO) {
        return mapPolygonPointMapper.selectPage(pageReqVO);
    }

}